﻿using Application.WebAPI.Filter;
using Framework.Common;
using Framework.Common.Exceptions;
using Framework.Common.Paged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using YouTravel.DomainModels;
using YouTravel.DomainService;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;
using YouTravel.QueryModels;
using YouTravel.ViewModels.Core;
using YouTravel.ViewModels;
using Framework.Core;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// 用户API接口
    /// </summary>
    [RoutePrefix("api/user")]
    public class UserController : BaseController
    {
        readonly IUserService _userService;
        readonly DataResult _dataResult = new DataResult();
        readonly CommonMD5 _commonMd5 = CommonMD5.getInstance();

        public UserController(IUserService userService)
        {
            this._userService = userService;
        }
        /// <summary>
        /// 账号添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("accountadd")]
        public HttpResponseMessage AccountAdd([FromBody]UserViewModel user)
        {
            #region 验证
            if (user == null) throw new ArgumentException("传入参数不能为空");
            if (LoginUser.Type == UserType.SpecialLine && !LoginUser.IsSpecialManager)
                throw new AuthorizeException("专线操作员无此权限");
            #endregion
            _userService.AccountAdd(user, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 修改账号
        /// 注:只能修改手机号码
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("accountupdate")]
        public HttpResponseMessage AccountUpdate([FromBody]UserViewModel user)
        {
            #region 验证
            if (user == null) throw new ArgumentException("传入参数不能为空");
            if (LoginUser.ID != user.ID)
                throw new DataOperationPermissions("无此操作权限,只能修改自己信息");
            #endregion
            _userService.AccountUpdate(user, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 账号启用禁用
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("accountenabled")]
        public HttpResponseMessage AccountEnabled([FromBody]UserViewModel user)
        {
            _userService.AccountEnabled(user.ID, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 设置权限
        /// 注:不做限制 只要传入进来都存入
        ///    在获得权限列表时做控制
        /// </summary>
        /// <param name="user"></param>
        [HttpPost, Route("setauthorityaccount")]
        public HttpResponseMessage SetAuthorityAccount([FromBody]UserViewModel user)
        {
            _userService.SetAuthorityAccount(user, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 账号销户
        /// </summary>
        /// <param name="user"></param>
        [HttpPost, Route("accountcancellation")]
        public HttpResponseMessage AccountCancellation([FromBody]UserViewModel user)
        {
            _userService.AccountCancellation(user.ID, LoginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 得到 用户权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("getaccountauthority")]
        public HttpResponseMessage GetAccountAuthority([FromBody]UserViewModel user)
        {

            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _userService.GetAccountAuthority(user.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 根据登录人得到相应菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getmenubyloginuser")]
        [Obsolete("此方法已废弃")]
        public HttpResponseMessage GetMenuByLoginUser()
        {

            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _userService.GetMenuByLoginUser(LoginUser.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("getmodel")]
        public HttpResponseMessage GetModel([FromBody]UserViewModel user)
        {

            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = _userService.GetModel(user.ID);
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }
        /// <summary>
        /// 查询用户名
        /// </summary>
        /// <param name="queryCond">查询条件</param>
        /// <returns></returns>
        //[AllowAnonymous]
        [HttpPost, Route("getlist")]
        public HttpResponseMessage GetList([FromBody]PagedParam<UserQuery> queryCond)
        {
            queryCond.QueryDto.LoginUserId = LoginUser.ID;
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Data = _userService.GetList(queryCond);
            //return dataResult;
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);

        }
        /// <summary>
        /// 得到机构下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("getorgcombox")]
        public HttpResponseMessage GetOrgCombox()
        {
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Data = new
            {
                OrgCombox = _userService.GetOrgCombox(LoginUser.ID),
                IsIsSpecialManager = LoginUser.Type == UserType.Branch
            };
            return Request.CreateResponse(HttpStatusCode.OK, _dataResult);
        }

        /// <summary>
        /// 账号设置
        /// </summary>
        /// <param name="user">参数</param>
        /// <returns></returns>
        [HttpPost, Route("accountset")]
        public DataResult AccountSet([FromBody]UserSetViewModel user)
        {
            var loginUser = LoginUser;
            this._userService.AccountSet(loginUser.ID, user.phone, loginUser.ID);

            loginUser.Phone = user.phone;
            RedisHelp.RefreshLoginUserCache(loginUser);

            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return _dataResult;
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="user">参数</param>
        /// <returns></returns>
        [HttpPost, Route("changepassword")]
        public DataResult ChangePassword([FromBody]UserSetViewModel user)
        {
            var loginUser = LoginUser;
            this._userService.ChangePassword(loginUser.ID, _commonMd5.MD5(user.oldPwd), _commonMd5.MD5(user.newPwd), _commonMd5.MD5(user.newPwdConfirm), loginUser.ID);
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return _dataResult;
        }

        /// <summary>
        /// 账号设置之前 获取账号信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("accountsetingdetail")]
        public DataResult AccountSetingDetail()
        {
            var loginUser = LoginUser;
            var detail = new
            {
                Account = loginUser.Login,
                Name = loginUser.Name,
                Phone = loginUser.Phone,
                Branch = loginUser.Branch == null ? null : new
                {
                    ID = loginUser.Branch == null ? 0 : loginUser.Branch.ID,
                    Name = loginUser.Branch == null ? "" : loginUser.Branch.Name
                },
                Special = loginUser.Special == null ? null : new
                {
                    ID = loginUser.Special == null ? 0 : loginUser.Special.ID,
                    Name = loginUser.Special == null ? "" : loginUser.Special.Name
                }
            };
            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            _dataResult.Data = detail;
            return _dataResult;
        }

        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous, Route("forgetpassword")]
        public DataResult ForgetPassword([FromBody]ForgetPasswordViewModel p)
        {
            this._userService.ForgetPassword(p);

            _dataResult.Code = ResponseStatusCode.Success;
            _dataResult.Msg = BusinessResourceMessage.Success;
            return _dataResult;
        }
    }
}
